from django.db.models import Sum

from goods_info.models import GoodsInventoryModel


def get_inventory(goods_id, warehouse_id=0):
    '''
    如果获取某一个货品的当前库存，如果warehouse_id=0,查询所有仓库的库存
    '''

    sum_inventory = 0
    if warehouse_id == 0:  # 查询所有总和
        result = GoodsInventoryModel.objects.filter(goods_id=goods_id).aggregate(sum=Sum('cur_inventory'))
    else:
        result = GoodsInventoryModel.objects.filter(goods_id=goods_id, warehouse_id=warehouse_id).aggregate(
            sum=Sum('cur_inventory'))

    if result['sum'] and result['sum'] != 'None':
        sum_inventory = result['sum']

    return sum_inventory
